package com.uniknow.app.operate.impl;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.uniknow.app.bin.ImageText;
import com.uniknow.app.bin.ResponseBean;
import com.uniknow.app.bin.UserBean;
import com.uniknow.app.http.AsyncHttpResponseHandler;
import com.uniknow.app.http.RequestParams;
import com.uniknow.app.manager.UserManager;
import com.uniknow.app.operate.BaseListener;
import com.uniknow.app.operate.HttpPath;
import com.uniknow.app.operate.WixMessageOperate;
import com.uniknow.app.util.SynchttpClient;
import com.uniknow.app.util.Sysout;


public class WixMessageOperateImpl implements WixMessageOperate{
	private Context mContext;
	
	public WixMessageOperateImpl(Context context){
		this.mContext = context;
	}
	
	private SynchttpClient getClient(){
		return SynchttpClient.getClient();
	}

	@Override
	public void sendText(UserBean user,String msg, final BaseListener listener) {
		RequestParams params = new RequestParams();
		
		params.put("user.id", user.getUserid()+"");
		params.put("user.name", user.getUserName());
		params.put("user.passwd", user.getPassWord());
		
		params.put("msgBean.text", msg);
		
		params.put("wixBeans[0].id", "1");
		params.put("wixBeans[1].id", "2");
		
		getClient().post(HttpPath.WIX_SENDTEXT, params, new AsyncHttpResponseHandler(){
			@Override
			public void onFailure(Throwable error) {
				error.printStackTrace();
				Sysout.i(WixMessageOperateImpl.class,error.toString());
				listener.onError(error.getMessage());
			}
			
			@Override
			public void onSuccess(String content) {	
				Sysout.i(LoginOperateImpl.class,content);
				try{
					Gson gson = new Gson();
					ResponseBean rs = (ResponseBean) gson.fromJson(content, ResponseBean.class);
					if (rs.OK()) {
						listener.onSuccess(content);
						
					}else{
						listener.onError(rs.errorInfo());
					}
				}catch(JsonSyntaxException e){
					Sysout.i(LoginOperateImpl.class,e.getMessage());
					listener.onError(e.getMessage());
				}
			}
		});
		
	}
	
	@Override
	public void sendText2All(UserBean user,String msg, final BaseListener listener) {
		RequestParams params = new RequestParams();
		params.put("user.name", user.getUserName());
		params.put("user.passwd", user.getPassWord());
		
		params.put("msgBean.text", msg);
		
		getClient().post(HttpPath.WIX_SENDTEXT2ALL, params, new AsyncHttpResponseHandler(){
			@Override
			public void onFailure(Throwable error) {
				error.printStackTrace();
				Sysout.i(WixMessageOperateImpl.class,error.toString());
				listener.onError(error.getMessage());
			}
			
			@Override
			public void onSuccess(String content) {	
				Sysout.i(LoginOperateImpl.class,content);
				try{
					Gson gson = new Gson();
					ResponseBean rs = (ResponseBean) gson.fromJson(content, ResponseBean.class);
					if ((rs.OK())) {
						listener.onSuccess(content);
						
					}else{
						listener.onError(rs.errorInfo());
					}
				}catch(JsonSyntaxException e){
					Sysout.i(LoginOperateImpl.class,e.getMessage());
					listener.onError(e.getMessage());
				}
			}
		});
		
	}

	@Override
	public void sendImageText(UserBean user, ImageText msg,
			final BaseListener listener) {
		RequestParams params = new RequestParams();
		params.put("user.id", user.getUserid()+"");
		params.put("user.name", user.getUserName());
		params.put("user.passwd", user.getPassWord());
		
		params.put("msgBean.text", msg.getText());
		params.put("msgBean.imgId", msg.getImgServerId());
		
		params.put("wixBeans[0].id", "1");
		params.put("wixBeans[1].id", "2");
		
		getClient().post(HttpPath.WIX_SENDTEXTIMAGE, params, new AsyncHttpResponseHandler(){
			@Override
			public void onFailure(Throwable error) {
				error.printStackTrace();
				Sysout.i(WixMessageOperateImpl.class,error.toString());
				listener.onError(error.getMessage());
			}
			
			@Override
			public void onSuccess(String content) {	
				Sysout.i(LoginOperateImpl.class,content);
				try{
					Gson gson = new Gson();
					ResponseBean rs = (ResponseBean) gson.fromJson(content, ResponseBean.class);
					if ((rs.OK())) {
						listener.onSuccess(content);
						
					}else{
						listener.onError(rs.errorInfo());
					}
				}catch(JsonSyntaxException e){
					Sysout.i(LoginOperateImpl.class,e.getMessage());
					listener.onError(e.getMessage());
				}
			}
		});
	}

}
